package cn.gson.springboot.controller;

import cn.gson.springboot.model.pojos.Contract;
import cn.gson.springboot.model.pojos.Employee;
import cn.gson.springboot.model.service.ContractService;
import cn.gson.springboot.model.vo.ContractVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ContractController {
    @Autowired
    ContractService contractService;
    @Autowired
    HttpSession session;
    @RequestMapping("/emp-contract-list")
    @ResponseBody
    public Map<String,Object> EmpContractList(Integer page, Integer size,Integer value,Integer num,Integer vague,String content) {
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contract> con = contractService.selectEmpContract(emp.getDepartment().getDeptId(),emp.getEmpId(),value,num,vague,content);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",con);
        return map;
    }
    // 按客户Id查询机会
    @RequestMapping("/cus-contract-list-all")
    @ResponseBody
    public List<Contract> AllCusContractList(Integer cusId) {
        List<Contract> con = contractService.selectCusContract(cusId);
        return con;
    }
    // 按照合同订单编号查询该员工合同订单
    @RequestMapping("/emp-con-id")
    @ResponseBody
    public Contract EmpConList(Integer conId) {
        Contract con= contractService.selectEmpContractById(conId);
        return con;
    }
    //按状态查询
    @RequestMapping("/emp-contract-status-list")
    @ResponseBody
    public Map<String,Object> EmpContractListByStatus(Integer page,Integer size,Integer contractStatus){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contract> contract = contractService.selectEmpContractByStatus(emp.getDepartment().getDeptId(),contractStatus);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",contract);
        return map;
    }
    //按类型查询
    @RequestMapping("/emp-contract-classify-list")
    @ResponseBody
    public Map<String,Object> EmpContractListByClassify(Integer page,Integer size,Integer contractSort){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contract> contract = contractService.selectEmpContractByClassify(emp.getDepartment().getDeptId(),contractSort);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",contract);
        return map;
    }
    //按机会主题查询
    @RequestMapping("/emp-contract-theme")
    @ResponseBody
    public Map<String,Object> EmpContractByTheme(Integer page,Integer size,String contractTheme,HttpSession session){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contract> contract = contractService.selectEmpContractByTheme(emp.getDepartment().getDeptId(),contractTheme);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",contract);
        return map;
    }
    //按客户名称查询
    @RequestMapping("/emp-contract-customer-list")
    @ResponseBody
    public Map<String,Object> EmpContractByCustomerName(Integer page,Integer size,String customerName){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contract> contract = contractService.selectEmpContractByCustomerName(emp.getDepartment().getDeptId(),customerName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",contract);
        return map;
    }
    //计算合同订单类型分布
    @RequestMapping("/sorts-list")
    @ResponseBody
    public List<Map<String,Object>> sortsList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = contractService.computeSorts(emp.getDepartment().getDeptId());
        return map;
    }
    //计算合同订单付款方式分布
    @RequestMapping("/pay-style-list")
    @ResponseBody
    public List<Map<String,Object>> payStyleList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = contractService.computeStyle(emp.getDepartment().getDeptId());
        return map;
    }
    //计算合同订单签约时间分布
    @RequestMapping("/sign-time-list")
    @ResponseBody
    public List<Map<String,Object>> signDateList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = contractService.computeSignDate(emp.getDepartment().getDeptId());
        return map;
    }
    //计算合同订单结束时间分布
    @RequestMapping("/end-time-list")
    @ResponseBody
    public List<Map<String,Object>> endDateList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = contractService.computeEndDate(emp.getDepartment().getDeptId());
        return map;
    }

    //计算合同订单状态分布
    @RequestMapping("/con-status-list")
    @ResponseBody
    public List<Map<String,Object>> conStatusList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = contractService.computeConStatus(emp.getDepartment().getDeptId());
        return map;
    }

    @RequestMapping("/save-contract")
    @ResponseBody
    public Integer saveContract(@RequestBody ContractVo vo){
        return contractService.saveContract(vo);
    }

    //删除合同订单
    @RequestMapping("/delete-contract")
    @ResponseBody
    public void deleteContract(@RequestParam(value = "contractId") Integer contractId){
        System.out.println(contractId);
        contractService.deleteContract(contractId);
    }
    //查询合同订单的所有主题
    @RequestMapping("/contract-all-list")
    @ResponseBody
    public List<Contract> AllContractTheme() {
        List<Contract> contract = contractService.findAllContractTheme();
        return contract;
    }

    //查询全部有效的合同订单
    @RequestMapping("/find-valid-list")
    @ResponseBody
    public Map<String,Object> AllValidContractList(Integer page,Integer size){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contract> contract = contractService.findAllValidContract(emp.getDepartment().getDeptId());
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",contract);
        return map;
    }
    //查询未审批合同订单
    @RequestMapping("/find-agree-list")
    @ResponseBody
    public Map<String,Object> AllAppContractList(Integer page,Integer size){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contract> contract = contractService.findAllAppContract(emp.getDepartment().getDeptId());
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",contract);
        return map;
    }
    //查询已否决合同订单
    @RequestMapping("/find-refuse-list")
    @ResponseBody
    public Map<String,Object> AllRefContractList(Integer page,Integer size){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contract> contract = contractService.findAllRefuseContract(emp.getDepartment().getDeptId());
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",contract);
        return map;
    }
    //查询所有我方签约人
    @RequestMapping("find-all-our-singer")
    @ResponseBody
    public List<Contract> findAllOurSigner(){
        List<Contract> con = contractService.findAllOurSigner();
        return con;
    }
    // 根据合同订单单号查询
    @RequestMapping("/con-no")
    @ResponseBody
    public Contract selectContractByConNo(String conNo) {
        Contract con = contractService.selectContractByConNo(conNo);
        return con;
    }
    // 查询未完成的合同订单
    @RequestMapping("/con-implement-list")
    @ResponseBody
    public List<Contract> selectContractImplement(){
        List<Contract> con= contractService.selectContractImplement();
        return con;
    }
    // 查询过去30天完成的合同订单
    @RequestMapping("/con-past-thirty-list")
    @ResponseBody
    public List<Contract>  contractPastThirtyList(){
        List<Contract> con= contractService.selectContractPastThirty();
        return con;
    }
    // 查询未尽回款的合同订单
    @RequestMapping("/con-not-payment-list")
    @ResponseBody
    public List<Contract> selectContractNotPayment(){
        List<Contract> con= contractService.selectContractNotPayment();
        return con;
    }
}
